package com.banquetmanagementsystem.service.impl;

import com.banquetmanagementsystem.common.response.Result;
import com.banquetmanagementsystem.mapper.DictionaryMapper;
import com.banquetmanagementsystem.pojo.entity.Dictionary;
import com.banquetmanagementsystem.service.DictionaryService;
import jakarta.annotation.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典管理
 * @author tanghaihua
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {
    @Resource(name = "dictionaryMapper")
    private DictionaryMapper dictionaryMapper;

    @Override
    public Result<?> insertDictionary(Dictionary dictionary) {
        if (dictionary.getName() == null || "".equals(dictionary.getName())) {
            return Result.failed("字典名称不能为空");
        }
        try {
            Integer i = dictionaryMapper.insertDictionary(dictionary.getName(), dictionary.getDictionaryDescribe(), dictionary.getSort());
            return i == 0 ? Result.failed("新增字典失败") : Result.success("新增字典成功");
        } catch (DuplicateKeyException e) {
            return Result.failed("字典名称已存在");
        }
    }

    @Override
    public Result<?> insertDictionaryContext(Dictionary dictionary) {
        if (dictionary.getPId() == null || dictionary.getPId() == 0) {
            return Result.failed("父级字典不能为空或0");
        }
        if (dictionary.getName() == null || "".equals(dictionary.getName())) {
            return Result.failed("字典内容名称不能为空");
        }
        try {
            Integer i = dictionaryMapper.insertDictionaryContext(dictionary.getPId(), dictionary.getName(), dictionary.getDictionaryDescribe(), dictionary.getSort());
            return i == 0 ? Result.failed("新增字典内容失败") : Result.success("新增字典内容成功");
        } catch (DuplicateKeyException e) {
            return Result.failed("字典内容名称已存在");
        }
    }

    @Override
    public Result<?> deleteDictionaryById(Integer id) {
        Integer count = dictionaryMapper.deleteDictionaryById(id);
        return count == 0 ? Result.failed("字典不存在") : Result.success("删除字典成功,删除字典内容" + (count - 1) + "条");
    }

    @Override
    public Result<?> deleteDictionaryContextById(Integer id) {
        Integer count = dictionaryMapper.deleteDictionaryContextById(id);
        return count == 0 ? Result.failed("字典内容不存在") : Result.success("删除字典内容成功");
    }

    @Override
    public Result<?> getAllDictionary() {
        List<Dictionary> dictionaries = dictionaryMapper.getAllDictionary();
        return dictionaries.size() == 0 ? Result.failed("暂无数据字典") : Result.success(dictionaryMapper.getAllDictionary());
    }

    @Override
    public Result<?> getDictionaryById(Integer id) {
        Dictionary dictionary = dictionaryMapper.getDictionaryById(id);
        return dictionary == null ? Result.failed("字典不存在") : Result.success(dictionary);
    }

    @Override
    public Result<?> getAllDictionaryContextBypId(Integer pId) {
        List<Dictionary> dictionaryContexts = dictionaryMapper.getAllDictionaryContextBypId(pId);
        return dictionaryContexts.size() == 0 ? Result.failed("暂无字典内容") : Result.success(dictionaryContexts);
    }

    @Override
    public Result<?> getDictionaryContextById(Integer id) {
        Dictionary dictionaryContext = dictionaryMapper.getDictionaryContextById(id);
        return dictionaryContext == null ? Result.failed("字典内容不存在") : Result.success(dictionaryContext);
    }

    @Override
    public Result<?> updateDictionary(Dictionary dictionary) {
        Result<?> result = checkDictionary(dictionary);
        if (result!=null){
            return result;
        }

        try {
            Integer count = dictionaryMapper.updateDictionaryById(dictionary.getId(), dictionary.getName(), dictionary.getDictionaryDescribe(), dictionary.getSort());
            return count == 0 ? Result.failed("字典修改失败") : Result.success("字典修改成功");
        } catch (DuplicateKeyException e) {
            return Result.failed("字典名称已存在");
        }
    }

    @Override
    public Result<?> updateDictionaryContext(Dictionary dictionary) {
        Result<?> result = checkDictionary(dictionary);
        if (result!=null){
            return result;
        }
        try {
            Integer count=dictionaryMapper.updateDictionaryContextById(dictionary.getId(), dictionary.getName(), dictionary.getDictionaryDescribe(), dictionary.getSort());
            return count == 0 ? Result.failed("字典内容修改失败") : Result.success("字典内容修改成功");
        }catch (DuplicateKeyException e){
            return Result.failed("字典内容名称已存在");
        }
    }

    /**
     * 验证数据合法性
     * @param dictionary 数据
     * @return 结果
     */
    private static Result<?> checkDictionary(Dictionary dictionary) {
        if (dictionary.getId() == null) {
            return Result.failed("id不能为空");
        }
        if ("".equals(dictionary.getName()) || "".equals(dictionary.getDictionaryDescribe())) {
            return Result.failed("修改的内容不能为空串");
        }
        if (dictionary.getName() == null
                && dictionary.getDictionaryDescribe() == null
                && dictionary.getSort() == null) {
            return Result.failed("请输入修改内容");
        }
        return null;
    }
}
